package com.logicvoid.rosiesettingsz;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class LeftActivity extends Activity {
	
	String TAG = "LeftActivity";
		
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.main);

		
		// Test to verify root
		// Should output: output (uid=0, root, gid=0, root),
		// String test = cmd.su.runWaitFor("id").stdout;
		// Log.v("ShellTest", "Got output: " + test);
		
		// Old Launch from shell file code
		/*
		ShellCommand cmd = new ShellCommand();		
		
		try {
			cmd.sh.run("sh /system/etc/RosieUtility/LeftButton.sh");

		} catch (Exception e) {
			Log.e(TAG, "Exception in LeftActivity", e);
		}
		*/
		
		
		// #### Launch From Preferences Begin ####
		
		try 
		{
			// Retrive default package if nothing assigned to button
			String defaultPackage = getText(R.string.left_button_default_package).toString();
			
			// Retrieve package name from preferences
			String packageName = Preferences.getStringPreferenceValue("leftButtonPackageName", defaultPackage, getApplicationContext());
			
			PackageManager packageManager = getPackageManager();			
			Intent intent = packageManager.getLaunchIntentForPackage(packageName);
			
			if(null != intent)
			{
			startActivity(intent);					
			}
						
		} 
	
		catch (ActivityNotFoundException e)
		{			
			Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
			Log.e(TAG, "Exception in onCreate - ActivityNotFoundException", e);
		}
		catch (Exception e) 
		{			
			Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
			Log.e(TAG, "Exception in onCreate - Exception", e);
		}
				
		
		finish();

	}

}